﻿Imports AForge.Imaging.Filters
Imports AForge
Imports UCBase
Imports System.Drawing
Imports System.Drawing.Imaging

Public Class ImageClient
    Implements I_ImageExchange
    Implements I_LoadSave
    Private WithEvents SourceEvent As I_ImageExchange
    Private Backpic As New BackgroundCreator
    Private Mov As New Mover
    Private SourceBitmap As System.Drawing.Bitmap
    Private Convert As New UCBase.ColorConverter
    Public Event ImageReady(ByVal Image As System.Drawing.Bitmap) Implements I_ImageExchange.ImageOut1
    Private NetDialog As New NetworkClientSettings
    Delegate Sub DataReceived(ByVal Data As Byte())
    Private RXDat As DataReceived
    Dim Transport As New NetworkTransport
    Private WithEvents NetworkClient1 As New NetworkClient
    Public Sub New()
        InitializeComponent()
        Me.BackgroundImage = Backpic.CreatePic(Me.Width, Me.Height, Me.BackColor)
        Me.BackColor = Color.Transparent
        Mov.Connect(Me)
        RXDat = New DataReceived(AddressOf NewData)
        AddHandler NetworkClient1.DataReceived, AddressOf NetworkClient1_DataReceived
        'NetworkClient1.Connect()
        Timer1.Enabled = True
        'Timer3.Enabled = True
        'Timer4.Enabled = True
    End Sub

    Public Function GetSettings() As String Implements UCBase.I_LoadSave.GetSettings
        Return ""
    End Function

    Public Sub SetSettings(ByVal Settings As String) Implements UCBase.I_LoadSave.SetSettings

    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label6.Text = NetworkClient1.RemoteIP
        Label3.Text = NetworkClient1.RemotePort
        If NetworkClient1.Connected Then
            Led2.State = Led.LedState.Led_On
        Else
            Led2.State = Led.LedState.Led_Off
        End If
    End Sub
    Protected Overrides Sub Finalize()
        NetworkClient1.Dispose()
        MyBase.Finalize()
    End Sub
    Private Sub NewData(ByVal data As Byte())
        If Transport.Decode(data) Then
            If Transport.ContainImage Then
                RaiseEvent ImageReady(Transport.Image)
                Led3.State = Led.LedState.Led_On
                Timer2.Enabled = True
            End If
        End If
    End Sub
    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Timer2.Enabled = False
        Led3.State = Led.LedState.Led_Off
    End Sub
    Private Sub Button3D1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3D1.Click
        Timer3.Enabled = False
        NetworkClient1.Disconnect()
        NetDialog.TextBox1.Text = NetworkClient1.RemotePort
        NetDialog.TextBox2.Text = NetworkClient1.RemoteIP
        If NetDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            NetworkClient1.RemotePort = Val(NetDialog.TextBox1.Text)
            NetworkClient1.RemoteIP = NetDialog.TextBox2.Text
        End If
        NetworkClient1.Connect()
        Timer3.Enabled = True
    End Sub
    Private Sub NetworkClient1_DataReceived(ByVal Bytes() As Byte)
        Invoke(RXDat, New Object() {Bytes})
    End Sub
    Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
        Label11.Text = TrackBar1.Value.ToString & "ms"
        Timer3.Interval = TrackBar1.Value
    End Sub
    Private Sub ImageClient_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        Me.Width = 260
        Me.Height = 140
    End Sub

    Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
        If NetworkClient1.Connected Then
            NetworkClient1.Send(Transport.Encode("GetImage"))
        End If

    End Sub

    Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
        If NetworkClient1.Connected = False Then
            Timer3.Enabled = False
            NetworkClient1.Connect()
        Else
            Timer3.Enabled = True
        End If
    End Sub

End Class
